﻿using Raven;
using UnityEngine;
using Vector2 = Raven.Vector2;

public class SlugDisplay : UnityDisplayObject
{
    private ProjectileSlug _projectileSlug;


    public SlugDisplay(BaseGameEntity entity) : base(entity)
    {
        _projectileSlug = entity as ProjectileSlug;
    }

    public override void Start()
    {
        base.Start();

        _gameObject.SetActive(false);
    }


    public override void Update()
    {
        if (!_projectileSlug.isDead() && _projectileSlug.HasImpacted() && !_gameObject.activeSelf)
        {
            Vector2 pos = _projectileSlug.GetOrigin();
            Vector2 impactPoint = _projectileSlug.GetImpactPoint() - pos;

            _gameObject.transform.position = new Vector3((float) pos.x, (float) pos.y);
            _gameObject.AddComponent<MeshFilter>().mesh =
                MeshFactory.LineMesh(Vector3.zero, new Vector3((float) impactPoint.x, (float) impactPoint.y),
                    Color.red);
            MeshRenderer render = _gameObject.AddComponent<MeshRenderer>();
            render.material = new Material(Shader.Find("Sprites/Default"));
            _gameObject.SetActive(true);
        }
    }
}